﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OgonPhonesXML.Aastra.Serializer;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace OgonPhonesXML.Aastra
{
    public class BaseAastraPhone
    {
        
        protected XmlSerializer Serializer = null;
        protected Object Component = new Object();

        private AastraIPPhoneConfiguration ipphoneconfiguration = null;
        public AastraIPPhoneConfiguration IPPhoneConfigurations
        {
            get
            {
                if (ipphoneconfiguration == null)
                {
                    
                    ipphoneconfiguration = new AastraIPPhoneConfiguration();
                    Serializer = new XmlSerializer(typeof(AastraIPPhoneConfiguration));
                    Component = ipphoneconfiguration;
                }
                return ipphoneconfiguration;
            }
        }

        private AastraIPPhoneTextScreen ipphonetextscreen = null;
        public AastraIPPhoneTextScreen IPPhoneTextScreen
        {
            get
            {
                if (ipphonetextscreen == null)
                {
                    ipphonetextscreen = new AastraIPPhoneTextScreen();
                    Serializer = new XmlSerializer(typeof(AastraIPPhoneTextScreen));
                    Component = ipphonetextscreen;
                }
                return ipphonetextscreen;
            }
        }

        private AastraIPPhoneTextMenu ipphonetextmenu = null;
        public AastraIPPhoneTextMenu IPPhoneTextMenu
        {
            get
            {
                if (ipphonetextmenu == null)
                {
                    ipphonetextmenu = new AastraIPPhoneTextMenu();
                    Serializer = new XmlSerializer(typeof(AastraIPPhoneTextMenu));
                    Component = ipphonetextmenu;
                }
                return ipphonetextmenu;
            }
        }

        private AastraIPPhoneFormattedTextScreen ipphoneformattedtext = null;
        public AastraIPPhoneFormattedTextScreen IPPhoneFormattedText
        {
            get
            {
                if (ipphoneformattedtext == null)
                {
                    ipphoneformattedtext = new AastraIPPhoneFormattedTextScreen();
                    Serializer = new XmlSerializer(typeof(AastraIPPhoneFormattedTextScreen));
                    Component = ipphoneformattedtext;
                }
                return ipphoneformattedtext;
            }
        }

        private AastraIPPhoneInputScreen ipphoneinputscreen = null;
        public AastraIPPhoneInputScreen IPPhoneInputScreen
        {
            get
            {
                if (ipphoneinputscreen == null)
                {
                    ipphoneinputscreen = new AastraIPPhoneInputScreen();
                    Serializer = new XmlSerializer(typeof(AastraIPPhoneInputScreen));
                    Component = ipphoneinputscreen;
                }
                return ipphoneinputscreen;
            }
        }

        private AastraIPPhoneStatus ipphonestatus = null;
        public AastraIPPhoneStatus IPPhoneStatusMessage
        {
            get
            {
                if (ipphonestatus == null)
                {
                    ipphonestatus = new AastraIPPhoneStatus();
                    Serializer = new XmlSerializer(typeof(AastraIPPhoneStatus));
                    Component = ipphonestatus;
                }
                return ipphonestatus;
            }
        }
        private AastraIPPhoneExecute ipphoneexecute = null;
        public AastraIPPhoneExecute IPPhoneExecute
        {
            get
            {
                if (ipphoneexecute == null)
                {
                    ipphoneexecute = new AastraIPPhoneExecute();
                    Serializer = new XmlSerializer(typeof(AastraIPPhoneExecute));
                    Component = ipphoneexecute;
                }
                return ipphoneexecute;
            }
        }

        public String getXML()
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            String XmlizedString = null;
            MemoryStream memoryStream = new MemoryStream();
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.GetEncoding("ISO-8859-1"));
            Serializer.Serialize(xmlTextWriter, this.Component, ns);
            memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
            XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
            return XmlizedString;
        }
        public String getXML(bool status)
        {
            if (status == false)
            {
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", "");
                XmlWriterSettings writerSettings = new XmlWriterSettings();
                writerSettings.OmitXmlDeclaration = true;
                StringWriter stringWriter = new StringWriter();
                using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, writerSettings))
                {
                    Serializer.Serialize(xmlWriter, Component, ns);
                }
                string xmlText = stringWriter.ToString();
                return xmlText;
            }
            else
            {
                return getXML();
            }
        }
        private String UTF8ByteArrayToString(Byte[] characters)
        {

            UTF8Encoding encoding = new UTF8Encoding();

            String constructedString = encoding.GetString(characters);

            return (constructedString);

        }
    }
}
